/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on Apr 21, 2010, 12:13:22 PM
 */

package tp_imag.gui;

import ij.ImagePlus;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import tp_imag.gui.area.selection.AreaSelectionPanel;
import tp_imag.gui.filters.FiltersPanel;
import tp_imag.gui.selection.SelectionPanel;
import tp_imag.model.ActualImage;
import tp_imag.service.ImageProcessor;
import tp_imag.service.ServiceFactory;

/**
 *
 * @author gdenari
 */
public class MainPanel extends javax.swing.JPanel {

    private FiltersPanel filterPanel = new FiltersPanel();
    private AreaSelectionPanel areaSelectionPanel = new AreaSelectionPanel();
    private SelectionPanel selPanel = new SelectionPanel();
    private int pantallaNro = 0;

    /** Creates new form MainPanel */
    public MainPanel() {
        initComponents();
        showSelPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelPaso = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tp_imag.Tp_imagApp.class).getContext().getResourceMap(MainPanel.class);
        jLabelPaso.setFont(resourceMap.getFont("jLabelPaso.font")); // NOI18N
        jLabelPaso.setText(resourceMap.getString("jLabelPaso.text")); // NOI18N
        jLabelPaso.setName("jLabelPaso"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelPaso, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelPaso, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 462, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 191, Short.MAX_VALUE)
        );

        jPanel3.setName("jPanel3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(270, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 191, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 191, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 120, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 120, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        if (pantallaNro == 1) {
            if (!validateSelectionScreen()) {
                JOptionPane.showMessageDialog(this, "La imagen seleccionada no es valida", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            ActualImage.getInstance().setPath(selPanel.getPath());
            jPanel2.remove(selPanel);
            this.showSelectAreaPanel();
            packFrame();
        } else {
        	if (pantallaNro == 2){
        		ImagePlus newImage = this.areaSelectionPanel.getNewImage();
            	jPanel2.remove(this.areaSelectionPanel);
            	this.showFilterPanel(newImage);
            }
        }
        


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        if (pantallaNro == 2) {
            jPanel2.remove(this.areaSelectionPanel);
            showSelPanel();
            packFrame();
        } else {
        	if (pantallaNro == 3){
            	jPanel2.remove(this.filterPanel);
            	showSelectAreaPanel();
            }
        }

    }//GEN-LAST:event_jButton2ActionPerformed


    private boolean validateSelectionScreen() {
        try {
            ImageProcessor proc = ServiceFactory.getInstance().getImageProcessorService();
            BufferedImage img = proc.openImage(selPanel.getPath());
            ActualImage.getInstance().setOriginal(img);
            if (img == null) {
                return false;
            } else {
                return true;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public void showFilterPanel(ImagePlus newImage) {
        jLabelPaso.setText("Paso 3: Filtrado y binarizacion de la imagen");
        pantallaNro = 3;
        jPanel2.setLayout(new GridLayout());

        // Sacar despues el new y ver porque no refrescaba los cambios de imagenes al volver atras !!!!!!!!!!!!!!!!
        this.filterPanel = new FiltersPanel();
        this.filterPanel.setVisible(true);
        filterPanel.loadScreen(newImage);
        jPanel2.add(this.filterPanel);
    }
    
    public void showSelectAreaPanel() {
        jLabelPaso.setText("Paso 2: Seleccione el sector de la imagen a utilizar");
        pantallaNro = 2;
        jPanel2.setLayout(new GridLayout());

        // Sacar despues el new y ver porque no refrescaba los cambios de imagenes al volver atras !!!!!!!!!!!!!!!!
        areaSelectionPanel = new AreaSelectionPanel();
        areaSelectionPanel.setVisible(true);
        areaSelectionPanel.refreshScreen();
        jPanel2.add(this.areaSelectionPanel);
    }


    private void packFrame() {
        Component comp = this;
        while (!(comp instanceof JFrame)) {
            comp = comp.getParent();
        }
        ((JFrame)comp).repaint();
        ((JFrame)comp).validate();
    }

    public void showSelPanel() {
        jLabelPaso.setText("Paso 1: Seleccione la imagen");
        //org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tp_imag.Tp_imagApp.class).getContext().getResourceMap(MainPanel.class);
        //jLabelPaso.setFont(resourceMap.getFont("jLabelPaso.font"));
        pantallaNro = 1;
        ActualImage.getInstance().setFiltered(null);
        jPanel2.setLayout(new GridLayout());
        this.areaSelectionPanel.setVisible(true);
        jPanel2.add(this.selPanel);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabelPaso;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables

}
